package com.runa.monitor.platinterface.netservice.equipment;

import com.runa.monitor.comm.dto.*;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.backwork.impl.SelfTaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.rccprotocol.dto.daoheng.DaoHengRequestData;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author diandian
 * @Date 2021/11/1 14:37
 */
@Slf4j
public class DaoHengDriver extends NetEqu<String> {
    private String electricSerial;

    public DaoHengDriver(String serial, Integer timeout, String electricSerial) {
        super(serial, timeout);
        this.electricSerial = electricSerial;
    }


    public void readElectricityMeter() {
        DaoHengRequestData requestData = new DaoHengRequestData(serial());
        requestData.readElectric(electricSerial);
        sendRequest(requestData, new SelfTaskFunction(serial()));
    }

    @Override
    public void readNodeEqu(List<Node> nodeList, TaskFunction function, EquData.EquDataType type) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        switch (type) {
            case VALVE:
                readHouseValve(nodeList, function);
                break;
            default:
        }
    }

    private void readHouseValve(List<Node> valves, TaskFunction function) {
        for (Node node : valves) {
            DaoHengRequestData requestData = new DaoHengRequestData(serial());
            requestData.readHouseValve(node.getSerial());
            requestData.setRemain(Collections.singletonList(node));
            sendRequest(requestData, function);
        }
    }


    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void valveOnOffControl(List<Node> valves, boolean ifLock, Integer opening, TaskFunction function) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, valves);
            return;
        }
        for (Node valve : valves) {
            if (valve != null) {
                DaoHengRequestData requestData = new DaoHengRequestData(this.serial());
                requestData.houseValveOpen(valve.getSerial(), opening);
                requestData.setRemain(valve);
                sendRequest(requestData, function);
            }
        }
    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> valveSerial, Integer model, Map param, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitModel(List<Node> valveSerial, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valveSerial, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valveSerial, Node meterSerial, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }

//    @Override
//    public Meter findMeter(String serial) {
//        return null;
//    }
}